let glob = require('glob');
let path = require('path');

let mix = require('laravel-mix');

//const {env}  = require('minimist')(JSON.parse(process.env.npm_config_argv).original);

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.setPublicPath('public/assets/');

mix.setResourceRoot('/assets/');

// 编译所有模块
glob.sync('modules/*/*/assets/js/*.js').forEach(function (entry) {
  let extname = path.extname(entry);
  let basename = path.basename(entry, path.extname(entry));

  // 获取模块名及功能目录名
  let dirname = path.dirname(entry);
  let arr = dirname.split('/');
  let len = arr.length;
  let moduleName = arr[len - 4] + '.' + arr[len - 3];
  console.log(dirname);

  // 组合输入目录及文件名
  let pathname = 'js/' + moduleName.toLowerCase() + '.' + basename + extname;
  mix.js(entry, pathname);
});

glob.sync('modules/*/*/assets/sass/*.scss').forEach(function (entry) {
  let extname = path.extname(entry);
  let basename = path.basename(entry, path.extname(entry));

  // 获取模块名及功能目录名
  let dirname = path.dirname(entry);
  let arr = dirname.split('/');
  let len = arr.length;
  let moduleName = arr[len - 4] + '.' + arr[len - 3];

  let pathname = 'css/' + moduleName.toLowerCase() + '.' + basename + '.css';
  mix.sass(entry, pathname);
});

mix.sass('resources/assets/sass/app.scss', 'css');

mix.extract(['vue', 'lodash', 'axios', 'element-ui'], 'js/vendor.js');


if (mix.inProduction) {
  mix.version();
}